package com.treasure.mall.zbqmallmanager.controller.community;

import com.treasure.mall.biz.dto.community.CommunityArticleSaveDTO;
import com.treasure.mall.biz.dto.community.CommunityArticleSearchCondition;
import com.treasure.mall.biz.entity.community.CommunityArticlePO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.community.CommunityArticleService;
import com.treasure.mall.biz.service.core.community.CommunityCommentService;
import com.treasure.mall.biz.service.core.user.UserService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.ienum.base.CodeDescEnumHelper;
import com.treasure.mall.zbq.common.ienum.community.CommunityEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallmanager.controller.community
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/7/30 11:00 上午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@Controller
@RequestMapping("/community")
public class CommunityController {
    @Autowired
    private CommunityArticleService articleService;
    @Autowired
    private CommunityCommentService commentService;
    @Autowired
    private UserService userService;


    @GetMapping("/list")
    public String list(
            @ModelAttribute("condition") CommunityArticleSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<CommunityArticlePO> sessionPOS = articleService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", sessionPOS.getTotalElements());
        model.addAttribute("totalPages", sessionPOS.getTotalPages());
        model.addAttribute("list", sessionPOS.getContent());

        return "community/list";
    }

    @PostMapping("/edit")
    @ResponseBody
    public ApiResult edit(CommunityArticleSaveDTO dto, String mobile) {
        if (dto.getArticleId() > 0) {
            CommunityArticlePO po = articleService.findById(dto.getArticleId());
            if (po == null) {
                return ApiResult.error("文章不存在或已被删除");
            }
            dto.setUser(po.getAuthor());
        }
        UserPO user = userService.findByUsername(mobile);
        if (user == null) {
            return ApiResult.error("作者不存在，请选择作者");
        }
        dto.setUser(user);
        articleService.save(dto);
        return ApiResult.ok();
    }

    @GetMapping("/detail")
    @ResponseBody
    public ApiResult detail(Long id) {
        CommunityArticlePO po = articleService.findById(id);
        return ApiResult.ok(po);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ApiResult delete(Long id) {

        CommunityArticlePO po = articleService.findById(id);
        if (po == null) {
            return ApiResult.error("文章不存在或已被删除");
        }
        articleService.delete(po.getAuthor().getUserId(), id);
        return ApiResult.ok();
    }


    /**
     * 审核
     *
     * @param id
     * @return
     */
    @PostMapping("/audit")
    @ResponseBody
    public ApiResult audit(Long id, int auditCode, String reason) {
        CommunityArticlePO po = articleService.findById(id);
        if (po == null) {
            return ApiResult.error("文章不存在或已被删除");
        }
        CommunityEnums.AuditStatus auditStatus = CodeDescEnumHelper.getEnumTypeByCode(CommunityEnums.AuditStatus.class, auditCode);
        articleService.modifyCommunityArticleAuditStatus(id, auditStatus, reason);
        return ApiResult.ok();
    }


}
